package lumis.service.comment;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import lumis.content.table.ContentTableDeleteDataProcessActionHandler;
import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.dao.jdbc.ITransactionJdbc;
import lumis.util.CollectionExecutor;
import lumis.util.JdbcUtil;
import lumis.util.query.QueryBase;

/**
 * Custom delete process action handler for comment that also
 * updates the total stored in the <code>lum_CommentTotal</code> table.
 * 
 * @version $Revision: 10272 $ $Date: 2009-03-30 13:20:23 -0300 (Mon, 30 Mar 2009) $
 * @since 4.0.10
 */
public class DeleteRelationCommentProcessActionHandler extends ContentTableDeleteDataProcessActionHandler
{
	/**
	 * Stores the targetContentLocaleIds being affected by the deletion.
	 */
	private Collection<String> targetContentLocaleIds = new HashSet<String>();
	
	/**
	 * Before deleting the information from database, 
	 * obtain the affected targetContentLocaleIds, storing them in {@link #targetContentLocaleIds},
	 * so it is possible to do an update in <code>lum_CommentTotal</code> later.
	 * 
	 * @version $Revision: 10272 $ $Date: 2009-03-30 13:20:23 -0300 (Mon, 30 Mar 2009) $
 	 * @since 4.0.10
	 */
	@Override
	protected void preDelete(QueryBase queryBase) throws PortalException
	{
		super.preDelete(queryBase);
	
		String[] commentIds = (String[]) getParameter("commentId");
		ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
		final Connection connection = daoTransactionJdbc.getConnection();
		
		CollectionExecutor<String> executor = new CollectionExecutor<String>(Arrays.asList(commentIds));
		executor.execute(new CollectionExecutor.IIterationCommand<String>()
		{
			public void execute(List<String> subItems) throws PortalException
			{
				try
				{
					PreparedStatement statement = connection.prepareStatement(
							"select distinct targetContentLocaleId " +
							"from lum_Comment where status=1 and " +
							JdbcUtil.generateInClause("commentId", subItems.size()));
					
					try
					{
						for (int i = 0;  i < subItems.size(); i++)
							statement.setString(i + 1, subItems.get(i));
						
						ResultSet resultSet = statement.executeQuery();
						try
						{
							while(resultSet.next())
							{
								targetContentLocaleIds.add(resultSet.getString("targetContentLocaleId"));
							}
						}
						finally
						{
							resultSet.close();
						}
					}
					finally
					{
						statement.close();
					}
				}
				catch(SQLException e)
				{
					throw new UnexpectedException(e);
				}
			}
		});
	}

	/**
	 * After deleting the information from database it calls 
	 * {@link SetCommentTotalInfoProcessActionHandler#updateCommentTotal(String, String, lumis.util.ITransaction)} 
	 * using the {@link #targetContentLocaleIds} obtained in {@link #preDelete(QueryBase)}.
	 * 
	 * @version $Revision: 10272 $ $Date: 2009-03-30 13:20:23 -0300 (Mon, 30 Mar 2009) $
 	 * @since 4.0.10
	 */
	@Override
	protected void postDelete() throws PortalException
	{
		super.postDelete();
		
		for (String targetContentLocaleId: targetContentLocaleIds)
		{
			SetCommentTotalInfoProcessActionHandler.updateCommentTotal(
					douiContext.getRequest().getServiceInstanceConfig().getId(), 
					targetContentLocaleId, transaction);
		}
	}
}
